$(function () {
    const $number = $("input:eq(0)");
    const $number1 = $("input:eq(1)");
    const $status = $("input:eq(5)");
    const $company = $("input:eq(2)");
    const $username = $("input:eq(3)");
    const $userphone = $("input:eq(4)");
    const $labelNumber = $("label:eq(0)");
    const $res = $("#res,#res-header");

    let timeout;
    let json;
    let flag = false;
    let findingNumber;
    let findByNumber = function () {
        $number.val($number.val().trim());
        if (timeout === undefined) timeout = setTimeout(function () {flag = false; timeout = undefined}, 30000);
        if (findingNumber === $number.val() && flag) {layer.msg('请勿重复提交同一内容');return false;}
        findingNumber = $number.val();
        if (!test_ipt(testNumber, $number, $labelNumber)) {layer.msg('快递单号格式有误');return false;}
        let windowId = layer.load();
        $.getJSON("getByNumber.do", {
            number: $number.val()
        }, function (data) {
            layer.close(windowId);
            layer.msg(data.message);
            flag = true;
            if (data.status === 0) {
                json = data.data;
                $number1.val(json.number);
                $company.val(json.company);
                $username.val(json.username);
                $userphone.val(json.userphone);
                $status.val(json.status);
                $res.fadeIn(1000);
            }
        });
        return false;
    };

    let keyPressEvent = function (key) {
        if (key.key === 'Enter') {
            findByNumber();
            onkeypress = undefined;
        }
    };

    $number.bind('input propertychange', function () {
        onkeypress = keyPressEvent;
        test_ipt(testNumber, $number, $labelNumber);
    })

    $("span:eq(1)").click(findByNumber);

    $("span:eq(3)").click(function () {
        let windowId = layer.load();
        $.getJSON("/admin/views/express/delete.do", {'id': json.id}, function (data) {
            layer.close(windowId);
            layer.msg(data.message);
            if (data.status === 0) {
                flag = false;
                $("input").val('');
                $res.hide();
            }
        });
        return false;
    });
});